﻿
Partial Class pages_files_Create
    Inherits CPage

    Public ReadOnly Property Name As String
        Get
            Return CWeb.RequestStr("name")
        End Get
    End Property

    Protected Overrides Sub PageInit()
        If CWeb.RequestBool("attach") Then
            Dim product As CProduct = CProduct.Cache.GetById(CWeb.RequestInt("productId"))
            If IsNothing(product) Then Throw New Exception("Product not found")

            Dim file As New CFile
            file.FileName = Name
            file.FileProductId = product.ProductId
            file.FileSize = 0
            file.SaveWithoutAudit()

            CWeb.Redirect("duplicates.aspx")
        End If


        MenuSelected = "Files"
        AddMenuSide("Duplicates", CSitemap.Duplicates)
        AddMenuSide("Find/Create")

        ddSpeaker.DataSource = CSpeaker.Cache
        ddSpeaker.DataBind()
        ddSpeaker.ValueInt = ESpeaker.MikeConnell
        ddSpeaker.NavigateUrl = CSitemap.SpeakerAdd
        ddSpeaker.HyperlinkText = "Add Speaker..."

        lnkFile.Text = Name
        lnkFile.NavigateUrl = CConfig.DropboxUrl(Name)
        lnkFile.Hyperlink.Target = "_blank"

        txtName.Text = IO.Path.GetFileNameWithoutExtension(Name)

        txtCreated.ShowAmPm = True

        If Page.IsPostBack Then Exit Sub

        Dim parts As String() = IO.Path.GetFileNameWithoutExtension(Name).Split("_")
        If parts.Length = 4 Then
            txtName.Text = parts(0)

            For Each i As CSpeaker In CSpeaker.Cache
                If i.SpeakerName.ToLower.Trim = parts(1).Trim.ToLower Then
                    ddSpeaker.ValueInt = i.SpeakerId
                    Exit For
                End If
            Next

            Dim dmy As String() = parts(2).Split("-")
            If dmy.Length <> 3 Then dmy = parts(2).Split(".")
            If dmy.Length = 3 Then
                Dim d As String = dmy(0)
                Dim m As String = dmy(1)
                Dim y As String = dmy(2)
                If y.Length = 2 Then y = "20" & y
                Try
                    txtCreated.ValueDate = New DateTime(y, m, d)
                Catch
                End Try
            End If

            With parts(3).Trim.ToLower.Replace(".", "")
                If .Equals("am") Then
                    txtCreated.ValueDate = txtCreated.ValueDate.AddHours(1)
                ElseIf .Equals("pm") Then
                    txtCreated.ValueDate = txtCreated.ValueDate.AddHours(12)
                End If
            End With
        End If
    End Sub

    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
        Dim d As DateTime = txtCreated.Value
        Dim n As String = txtName.Text.ToLower.Trim

        Dim matching As New CProductList
        For Each i As CProduct In CProduct.Cache.Sermons
            If i.ProductCreated.AddDays(5) > d AndAlso i.ProductCreated.AddDays(-5) < d Then
                matching.Add(i)
            ElseIf n.Length > 0 AndAlso i.ProductName.ToLower.Contains(n) Then
                matching.Add(i)
            End If
        Next

        CDropdown.BlankItem(dd, "-- Attach file to existing Sermon --")
        For Each i As CProduct In matching
            CDropdown.Add(dd, i.DateString & " - " & i.ProductName, i.ProductId)
        Next
        dd.Attributes.Add("onchange", "document.location='create.aspx?attach=true&name=" & CSitemap.Encode(Name) & "&productId='+value")

        ctrlProducts.Display(matching, True, False, False, True, False, False)
        ctrlProducts.Visible = True

        btnSave.Enabled = True
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        Dim p As New CProduct
        p.ProductCreated = txtCreated.ValueDate
        p.ProductSpeakerId = ddSpeaker.ValueInt
        p.ProductIsActive = True
        p.ProductLocationId = ELocation.Baycity
        p.ProductName = txtName.Text
        p.ProductTypeId_ = EProductType.Sermon
        p.SaveWithoutAudit()

        Dim f As New CFile
        f.FileName = Name
        f.FileProductId = p.ProductId
        f.FileSize = 0
        f.SaveWithoutAudit()

        Response.Redirect(CSitemap.Duplicates)
    End Sub
End Class
